package helpers;

import java.text.DecimalFormat;

/*
 * Cette classe fournit des methodes qui facilite la récupération, la vérification de données pour les servlets
 */

public class ServletHelper {

    public static final int CATEGORIES_PAR_PAGE = 3;
    public static final int JEUXVIDEO_PAR_CATEGORIE = 4;
    public static final int JEUXVIDEO_PAR_PAGE = 12;
    public static final int JEUXVIDEO_PAR_ACCUEIL = 10;

    private ServletHelper() {
    }

    public static boolean verifyStrings(String... params) {
        for (String param : params) {
            if (!verifyString(param)) {
                return false;
            }
        }

        return true;
    }

    public static boolean verifyString(String param) {
        return (param != null && !param.isEmpty());
    }

    public static int getStart(String value, int total) {
        int debut = getIntegerValue(value) - 1;

        if (debut < 0 || debut >= total) {
            debut = 0;
        }

        return debut;
    }

    public static int getTotalPages(int total, int elements) {
        return (int) Math.ceil((double) total / elements);
    }

    public static int getIntegerValue(String value) {
        if (verifyString(value)) {
            try {
                return Integer.parseInt(value);
            } catch (NumberFormatException e) {
            }
        }

        return 0;
    }

    public static String getValue(String value, String defaultValue) {
        if (!verifyString(value)) {
            value = defaultValue;
        }

        return value;
    }

    public static String roundTwoDecimals(double decimal) {
        return new DecimalFormat("###,###.##").format(decimal);
    }
}
